<?php


namespace Backend\Modules\Video\Controllers;

use Common\library\Common;
use Library\Constant\Constant;
use Phalcon\Paginator\Adapter\QueryBuilder as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Forms\FormSearchVideoFile;

class AjaxVideoFileController extends \BackendController
{
	public function loadVideoFileAction()
	{
		$this->view->setTemplateAfter('empty');

        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        //$videoFiles = VideoFile::find();
        $videoFilesBuilder = $this->modelsManager->createBuilder()
                ->from(array('videoFile' => '\Backend\Modules\Video\Models\VideoFile'))
                ->groupBy("videoFile.id")
                ->orderBy('videoFile.id');

        $formSearchVideoFile = new FormSearchVideoFile();

        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'builder'  => $videoFilesBuilder,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                'rangeLength' => 5,
                'urlMask'     => '&page={%page_number}',
            )
        );
        $this->view->objectType = $this->request->get('object_type');
        $this->view->objectId = $this->request->get('object_id');
        $this->view->formSearchVideoFile = $formSearchVideoFile;
        $this->view->pager = $pager;
        $this->view->render("ajax-video-file", "modal/modal_video_file");
	}

    public function searchVideoFileAction()
    {
        $json['success'] = false;

        if($this->request->isAjax())
        {
            $this->view->setTemplateAfter('empty');

            $currentPage = abs($this->request->getPost('page', 'int', 1));

            $sqlWhere = '1 = 1';
            $conditionsWhere = array();

            if($this->request->hasPost('id') && $this->request->getPost('id'))
            {
                $sqlWhere .= ' AND videoFile.id = :id:';
                $conditionsWhere['id'] = $this->request->getPost('id');
            }

            if($this->request->hasPost('name') && $this->request->getPost('name'))
            {
                $sqlWhere .= ' AND videoFile.name LIKE :name:';
                $conditionsWhere['name'] = '%' . $this->request->getPost('name') . '%';
            }

            if($this->request->hasPost('quanlity') && $this->request->getPost('quanlity'))
            {
                $sqlWhere .= ' AND videoFile.quanlity = :quanlity:';
                $conditionsWhere['quanlity'] = $this->request->getPost('quanlity');
            }

            if($this->request->hasPost('source_server') && $this->request->getPost('source_server'))
            {
                $sqlWhere .= ' AND videoFile.source_server = :source_server:';
                $conditionsWhere['source_server'] =  $this->request->getPost('source_server');
            }

            $videoFiles = $this->modelsManager->createBuilder()
                ->from(array('videoFile' => '\Backend\Modules\Video\Models\VideoFile'))
                ->groupBy("videoFile.id")
                ->where($sqlWhere , $conditionsWhere)
                ->orderBy('videoFile.id');

            if ($currentPage == 0) {
                $currentPage = 1;
            }

            $pager = new Pager(
                new Paginator(array(
                    'builder'  => $videoFiles,
                    'limit' => Constant::ITEM_PER_PAGE_20,
                    'page'  => $currentPage,
                )),
                array(
                    'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                    'rangeLength' => 5,
                    'urlMask'     => '&page={%page_number}',
                )
            );
            $this->view->pager = $pager;
            $this->view->render("ajax-video-file", "result_search_video_file");

            $json['success'] = true;
            $json['html'] = $this->view->getContent();
            $this->response->setContent(json_encode($json));
            //Return the response
            return $this->response->send();
        }
        echo json_encode($json);
    }

    public function addVideoFilesToObjectAction()
    {
        if(!$this->request->has('object_type'))
        {
            return $this->response->redirect($this->request->getHTTPReferer());
        }
        $objectType  = $this->request->get('object_type');

        switch($objectType)
        {
            case 'video':
                if($this->request->getPost('selectVideoFile'))
                {
                    $videoId = $this->request->get('object_id');
                    $video = \Backend\Modules\Video\Models\Video::findFirstById($videoId);
                    if(!$video)
                    {
                        return $this->response->redirect($this->request->getHTTPReferer());
                    }

                    $selectVideoFile = $this->request->getPost('selectVideoFile');
                    $data = array();
                    foreach($selectVideoFile as $videoFileId)
                    {
                        $checkVideoFileHaveVideo = \Backend\Modules\Video\Models\VideoFileLink::find(array(
                            "video_id = $videoId AND video_file_id = $videoFileId"
                        ))->count();

                        if($checkVideoFileHaveVideo <= 0)
                        {
                            $selectVideoFile = \Backend\Modules\Video\Models\VideoFile::findFirstById($videoFileId)->toArray();

                            if($selectVideoFile)
                            {
                                $videoFileLink = new \Backend\Modules\Video\Models\VideoFileLink();
                                $videoFileLink->setVideoId($videoId);
                                $videoFileLink->setVideoFileId($videoFileId);
                                if(!$videoFileLink->save())
                                {

                                }
                            }
                        }
                    }
                }

                break;
        }
        return $this->response->redirect($this->request->getHTTPReferer());
    }
}